<?php
/* 
 * 系统名称：以厘php框架
 * 官方网址：https://kuosoft.com
 * 版权所有：2009-2025 阔软工作室 (https://kuosoft.com) 并保留所有权利。 
 * 代码协议：开源代码协议 Apache License 2.0 详见 http://www.apache.org/licenses/
 */
define("Residentmemory", "build");
define('WWW',str_ireplace('\\','/', dirname(__FILE__) . '/'));
include "index.php";
$_POST['filelist'] = [];
function ELiSao($dir, $virtual = false)
{
    $ds = DIRECTORY_SEPARATOR;
    $dir = $virtual ? realpath($dir) : $dir;
    $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir;
    if (is_dir($dir) && $handle = opendir($dir)) {
        while ($file = readdir($handle)) {
            if ($file == '.' || $file == '..' || $file=='.DS_Store') {
                continue;
            } elseif (is_dir($dir . $ds . $file)) {
                ELiSao($dir . $ds . $file);
            } else {
                $_POST['filelist'][] = str_ireplace('\\','/',$dir . $ds . $file);
            }
        }
        closedir($handle);
        return true;
    }
    return false;
}

$buildPHP = WWW.'dist/';
$PHPindex = $buildPHP.'index.php';
ELiCreate($buildPHP);
ELiCreate($buildPHP.'ELikj/');


copy(WWW.'favicon.ico',$buildPHP.'favicon.ico');
copy(WWW.'index.php',$PHPindex);
$PHPN = file_get_contents($PHPindex);
$classneirong = "";
ELiSao(WWW.'ELikj/Controller');
$BAOHANMULU = [];

$USE =[];


foreach($_POST['filelist'] as $files){

    if( strstr($files, "Class.php") !==false ){
        
        preg_match_all('#/Controller/(.*).Class.php#iUs', $files, $nameke );
        $BAOHANMULU[$nameke[1][0]] = $nameke[1][0];
        $fileneirong = file_get_contents($files);
        $fileneirong = str_ireplace(["\r\n","\r"],["\n","\n"],$fileneirong);
        $nlist = explode("\n", $fileneirong);
        $keshi = false;
        foreach($nlist as $weo){
            if($keshi){
                $classneirong.="\n".$weo;
            }else{
                if((strstr($weo,'use ')!==false ||  strstr($weo,'USE ')!==false )&& strstr($weo,';')!==false){
                    $USE[trim($weo)] = trim($weo);
                }
                
                if(strstr($weo, "class ELikj_") !==false){
                    $keshi = true;
                    $classneirong.="\n".$weo;
                }
            }
        }
    }
}

$_POST['filelist'] = [];
ELiSao(WWW.'Tpl');

foreach($_POST['filelist'] as $wenjian){

    if( strstr($wenjian, ".php") !==false ){

        $wenjianx =  str_replace([WWW.'Tpl/','.php'],['',''],$wenjian);
        $wjlist = explode("/",$wenjianx);

        if(isset($BAOHANMULU[$wjlist[0]])){
            //存在文件值
            $fname = "function ELi_Function_".md5(  $wenjianx).'($ELiConfig, $ELiMem, $features,$CANSHU, $SESSIONID, $LANG,$THIS,$plugin,$file){'."\n";
            $classneirong.="\n".$fname;

            $wnfen =  file_get_contents($wenjian);
            if( strstr($wnfen, "?>") !==false ){
                $classneirong.= '?>'.$wnfen."\n\n <?php \n";
            }else{
                $classneirong.= '?>'.$wnfen."\n ?><?php \n";
            }
            $classneirong.="}\n ";
        }

    }else{
        $nwenjian = str_replace(WWW,WWW.'dist/',$wenjian);
        ELiCreate($nwenjian);
        copy($wenjian,$nwenjian);
    }
}

$PHPN =  str_ireplace('/////#Class include#////',$classneirong,$PHPN );


$elifuncton = '
if (function_exists($FuanCtionName)) {
    return  $FuanCtionName($ELiConfig, $ELiMem, $features,$CANSHU, $SESSIONID, $LANG,$THIS,$plugin,$file);
}
';
$PHPN =  str_ireplace('/////#ELitpl#////',$elifuncton,$PHPN );
if(file_exists(WWW.'ELikj/composer.json')){
    copy(WWW.'ELikj/composer.json',WWW.'dist/ELikj/composer.json');
}
$_POST['filelist'] = [];
ELiSao(WWW.'ELikj/vendor');
foreach($_POST['filelist'] as $wenjian){
    $nwenjian = str_replace(WWW,WWW.'dist/',$wenjian);
    ELiCreate($nwenjian);
    copy($wenjian,$nwenjian);
}
if(count($USE) > 0 && (
    strstr($PHPN,"'Composer' => 1") !==false ||
    strstr($PHPN,"'Composer' => '1") !==false ||
    strstr($PHPN,"'Composer' => \"1") !==false
)){
    $PHPN =  str_ireplace('/////#Class USE#////',implode("\n",$USE),$PHPN );
}



file_put_contents($PHPindex,$PHPN);
echo " OK ";